//
//  AppTools.m
//  TourPlay
//
//  Created by dllo on 15/11/17.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "AppTools.h"
#import "AFNetworking.h"
#import "BaseViewController.h"

@implementation AppTools

+ (void)getAFNet:(NSString *)url block:(getAFNetBlock)block errorBlock:(Error)errorBlock
{
 
    NSString *url_string = url;
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        // 缓存的文件夹
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
        
        // 存储的沙盒路径
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//        NSLog(@"%@", path_doc);
        // 归档
        [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
        
        
        block(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
        
        // 缓存的文件夹
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        block(result);
        
        NSLog(@"失败==== %@",error);
        errorBlock(error);
        
    }];
    
    

}
+ (void)compareSumWithArray:(NSMutableArray *)arr
{
        for (NSInteger i = 0; i < arr.count - 1; i++) {
            for (NSInteger j = 0; j < arr.count - 1 - i; j++) {
                if ([[arr objectAtIndex:j] intValue] > [[arr objectAtIndex:j + 1] intValue]) {
                    [arr exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
                }
            }
        }
}
+ (float)widthForContent:(NSString *)content
{
    CGSize size = CGSizeMake(1000, 44);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:NAVG_HOME_FONT, NSFontAttributeName, nil];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return (float)frame.size.width;
}
+ (float)heightForContent:(NSString *)content width:(float)width font:(float)font
{
//    NSLog(@"%@ - %f - %f", content, width, font);
    CGSize size = CGSizeMake(width, 1000);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font], NSFontAttributeName, nil];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return (float)frame.size.height;

}
// 时间戳转换时间
+ (NSString *)timestampExchange:(NSInteger)time
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}
// 得到当前时间
+ (NSString *)getNowTime
{
    // 获取当前时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY:MM:dd HH:mm:ss"];
    NSString *date = [formatter stringFromDate:[NSDate date]];
    [formatter release];
    
    return date;
}
@end
